﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace System
{
    public class AppSettings
    {
        public static string InputQueueName
        {
            get { return TryGetValueFromConfig(_ => _, () => @"FormatName:Direct=OS:localhost\private$\eventbroker", supressKey: "eventBrokerQueueName"); }
        }

        #region 私有方法

        private static T TryGetValueFromConfig<T>(Func<string, T> parseFunc, Func<T> defaultTValueFunc,
            string key = "", string supressKey = "")
        {
            try
            {
                if (!string.IsNullOrEmpty(supressKey) && !string.IsNullOrWhiteSpace(supressKey))
                {
                    key = supressKey;
                }

                var node = ConfigurationManager.AppSettings[key];
                return !string.IsNullOrEmpty(node) ? parseFunc(node) : defaultTValueFunc();
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("Error Reading web.config on AppSettings node: {0},", key) + ex.Message);
                return default(T);
            }
        }

        #endregion
    }
}
